package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Patient;
import com.jrtc.base.entity.bo.PatientExerciseprescription;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.vo.PatientCollectVo;
import com.jrtc.base.entity.vo.PrescriptionVo;
import com.jrtc.entity.vo.ExercisePrescriptionVo;
import com.jrtc.base.entity.vo.PatientExerciseprescriptionVo;
import com.jrtc.service.PatientExerciseprescriptionService;
import com.jrtc.service.PatientService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 患者运动处方表(PatientExerciseprescription)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("prescriptionExercise")
public class PatientExerciseprescriptionController {
    /**
     * 服务对象
     */
    @Resource
    private PatientExerciseprescriptionService patientExerciseprescriptionService;
    @Resource
    private PatientService patientService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public PatientExerciseprescription selectOne(Integer id) {
        return this.patientExerciseprescriptionService.queryById(id);
    }
    
    @RequestMapping("getPrescriptionList")
    public ResultDTO getPatientExerciseprescriptionList(@RequestBody PatientExerciseprescription patientExerciseprescription)
    {
        IPage<PatientExerciseprescription> ipage = patientExerciseprescriptionService.queryPatientExerciseprescriptionList(patientExerciseprescription);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertPrescription")
    public ResultDTO insertPatientExerciseprescription(@RequestBody PatientExerciseprescription patientExerciseprescription)
    {
        PatientExerciseprescription tmpPatientExerciseprescription = patientExerciseprescriptionService.insert(patientExerciseprescription);
        if (tmpPatientExerciseprescription != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getPrescriptionById")
    public ResultDTO getPatientExerciseprescriptionById(@RequestBody Map<String,Integer> map)
    {
        int patientExerciseprescriptionId = map.get("patientExerciseprescriptionId");
        PatientExerciseprescription patientExerciseprescription = patientExerciseprescriptionService.queryById(patientExerciseprescriptionId);
        if (patientExerciseprescription==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(patientExerciseprescription);
    }

    @RequestMapping("updatePrescription")
    public ResultDTO updatePatientExerciseprescription(@RequestBody PatientExerciseprescription patientExerciseprescription)
    {
        if (patientExerciseprescription==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (patientExerciseprescriptionService.update(patientExerciseprescription)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deletePrescription")
    public ResultDTO deletePatientExerciseprescription(@RequestBody Map<String,Integer> map)
    {
        int patientExerciseprescriptionId = map.get("patientExerciseprescriptionId");
        if (patientExerciseprescriptionService.deleteById(patientExerciseprescriptionId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 查询患者的运动处方记录
     * */
    @PostMapping("/prescriptionList")
    public ResultDTO selectPatientExerciseprescriptionList(HttpServletRequest request){
        Patient patient;
        try {
            patient = patientService.getLoginPatient(request);
        } catch (Exception e){
            return null;
        }

        if (patient != null){
            Integer patientId = patient.getPatientId();
            List<PatientExerciseprescriptionVo> patientExerciseprescriptionVoList;
            try {
                patientExerciseprescriptionVoList = patientExerciseprescriptionService.selectPatientExerciseprescriptionList(patientId);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }
            if (patientExerciseprescriptionVoList != null && patientExerciseprescriptionVoList.size() > 0){
                return ResultDTOBuilder.success(patientExerciseprescriptionVoList);
            }
            else {
                return ResultDTOBuilder.failureMsg("未查到患者运动处方信息");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("登录已过期，请重新登陆");
        }
    }

    /**
     * 创建患者运动处方信息
     * */
    @PostMapping("/insertPrescription")
    public ResultDTO insertPatientExerciseprescription(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Patient patient;
        try {
            patient = patientService.getLoginPatient(request);
        } catch (Exception e){
            return null;
        }

        if (patient == null){
            return ResultDTOBuilder.failureMsg("登陆已过期，请重新登录");
        }
        if (map.get("prescriptionId") != null && !map.get("prescriptionId").equals("")){
            Integer prescriptionId = Integer.parseInt(map.get("prescriptionId").toString());
            Integer patientId = patient.getPatientId();
            int i;
            try {
                i = patientExerciseprescriptionService.insertPatientExerciseprescription(prescriptionId, patientId);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }
            if (i > 0){
                return ResultDTOBuilder.success("创建成功");
            }
            else {
                return ResultDTOBuilder.failureMsg("创建失败");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }

    /**
     * 患者所有运动处方信息列表
     * */
    @PostMapping("/allExercisePrescriptions")
    public ResultDTO allExercisePrescriptions(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Integer patientId = 0;
        if (map.get("id") != null && !map.get("id").equals("")){
            patientId = Integer.parseInt(map.get("id").toString());
        }
        else {
            Patient patient;
            try {
                patient = patientService.getLoginPatient(request);
                patientId = patient.getPatientId();
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg("登录已过期");
            }
        }

        if (patientId == 0){
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }

        String name = null;
        if (map.get("name") != null && !map.get("name").equals("")){
            name = map.get("name").toString();
        }

        ExercisePrescriptionVo exercisePrescriptionVo;
        try {
            exercisePrescriptionVo = patientExerciseprescriptionService.allExercisePrescriptions(patientId, name);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (exercisePrescriptionVo != null){
            return ResultDTOBuilder.success(exercisePrescriptionVo);
        }
        else {
            return ResultDTOBuilder.failureMsg("查询失败");
        }
    }

    /**
     * 患者收藏的运动处方
     * */
    @PostMapping("/selectPatientCollect")
    public ResultDTO selectPatientCollect(HttpServletRequest request){
        Patient patient;
        try {
            patient = patientService.getLoginPatient(request);
        } catch (Exception e){
            return null;
        }

        List<PatientCollectVo> patientCollectVoList;
        try {
            patientCollectVoList = patientExerciseprescriptionService.selectPatientCollect(patient.getPatientId());
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (patientCollectVoList != null && patientCollectVoList.size() > 0){
            return ResultDTOBuilder.success(patientCollectVoList);
        }
        else {
            return ResultDTOBuilder.failureMsg("您还没有收藏运动处方");
        }
    }

    /**
     * 患者运动处方列表
     * */
    @PostMapping("/patientExercisePrescription")
    public ResultDTO patientExercisePrescription(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        String name = null;
        if (map.get("name") != null && !map.get("name").equals("")){
            name = map.get("name").toString();
        }
        List<PrescriptionVo> prescriptionVo;
        try {
            prescriptionVo = patientExerciseprescriptionService.patientExercisePrescription(id, name);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(prescriptionVo);
    }

    /**
     * 定制患者运动处方
     * */
    @PostMapping("/customMade")
    public ResultDTO customMade(HttpServletRequest request, @RequestBody PatientExerciseprescription patientExerciseprescription){
        int i;
        try {
            i = patientExerciseprescriptionService.customMade(patientExerciseprescription, request);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (i > 0){
            return ResultDTOBuilder.success("添加成功");
        }
        else {
            return ResultDTOBuilder.failureMsg("添加失败");
        }
    }

    /**
     * 查询待修改的患者运动处方
     * */
    @PostMapping("/selCustomMade")
    public ResultDTO selCustomMade(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        PatientExerciseprescription patientExerciseprescription = patientExerciseprescriptionService.selCustomMade(id);
        return ResultDTOBuilder.success(patientExerciseprescription);
    }

    /**
     * 修改患者运动处方
     * */
    @PostMapping("/saveCustomMade")
    public ResultDTO saveCustomMade(@RequestBody PatientExerciseprescription patientExerciseprescription){
        int i;
        try {
            i = patientExerciseprescriptionService.saveCustomMade(patientExerciseprescription);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (i > 0){
            return ResultDTOBuilder.success("修改成功");
        }
        else {
            return ResultDTOBuilder.failureMsg("修改失败");
        }
    }
}